//
//  CALayerExtension.swift
//  YYProject
//
//  Created by 陆文宇 on 2017/9/7.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import Foundation

extension UILabel {
    
    var underlineRange: Any! {
        set {
            var range = newValue as! NSRange
            let attr = NSMutableAttributedString(attributedString: attributedText!)
            
            if NSEqualRanges(range, NSMakeRange(0, 0)) {
                range = NSMakeRange(0, attr.length)
            }
            
            let underlineColor = attr.attribute(NSAttributedStringKey.foregroundColor, at: range.location, effectiveRange: nil)!
            attr.addAttributes([NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey.underlineColor: underlineColor], range: range)
            attributedText = attr
        }
        
        get {
            return nil
        }
    }
    
    open override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        if tag == 90 {
            layer.borderColor = UIColor(white: 0.9, alpha: 1).cgColor
        }
    }
    
}

extension UIImageView {
    open override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        if tag == 100 {
            layer.borderColor = UIColor.white.cgColor
        }
    }
}

extension UIButton {
    
    var underlineRange: Any! {
        set {
            var range = newValue as! NSRange
            let attr = NSMutableAttributedString(attributedString: attributedTitle(for: .normal)!)
            
            if NSEqualRanges(range, NSMakeRange(0, 0)) {
                range = NSMakeRange(0, attr.length)
            }
            
            let underlineColor = attr.attribute(NSAttributedStringKey.foregroundColor, at: range.location, effectiveRange: nil)!
            attr.addAttributes([NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey.underlineColor: underlineColor], range: range)
            setAttributedTitle(attr, for: .normal)
        }
        
        get {
            return nil
        }
    }
    
    open override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        if tag == 367 {
            layer.borderColor = mainColor.cgColor
        } else if tag == 9155 {
            layer.shadowColor = mainColor.cgColor
            layer.shadowOpacity = 0.5
        }
    }
    
}

extension CALayer {
    
    var borderUIColor: UIColor? {
        set {
            borderColor = newValue?.cgColor
        }

        get {
            return borderColor == nil ? nil : UIColor(cgColor: borderColor!)
        }
    }
    
    var shadowUIColor: UIColor? {
        set {
            shadowColor = newValue?.cgColor
        }
        
        get {
            return shadowColor == nil ? nil : UIColor(cgColor: shadowColor!)
        }
    }
    
}
